//
//  HTClassYearMonthTool.swift
//  QuitSmokingApp
//  
//  Created by wealon on 2024.
//  QuitSmokingApp.
//  
    

import UIKit
import PGDatePicker

class HTClassYearMonthTool: UIView {
    lazy var var_datePicker: PGDatePicker = {
        let var_picker = PGDatePicker(frame: .zero)
        var_picker.datePickerMode = .yearAndMonth
        var_picker.autoSelected = true
        var_picker.datePickerType = .segment
        var_picker.textColorOfSelectedRow = .white
        var_picker.textFontOfOtherRow = .systemFont(ofSize: 28, weight: .bold)
        var_picker.textFontOfSelectedRow = .systemFont(ofSize: 28, weight: .bold)
        var_picker.textColorOfOtherRow = UIColor(red: 89.0/255.0, green: 136.0/255.0, blue: 170.0/255.0, alpha: 1.0)
        var_picker.rowHeight = 59
        var_picker.delegate = self
        var_picker.language = "en"
        var_picker.showUnit = .none
        var_picker.backgroundColor = .clear
        return var_picker
    }()
    var BLOCK_selectDateBLOCK:((String) -> Void)?
    var var_isSelect = false
    static func ht_showDatePick(_ BLOCK_selectDateBLOCK: @escaping ((String) -> Void)) {
        let ht_view = HTClassYearMonthTool()
        ht_view.BLOCK_selectDateBLOCK = BLOCK_selectDateBLOCK
        ht_view.ht_showAnimated()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .black.withAlphaComponent(0.8)
        ht_setUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func ht_setUI() {
        self.addSubview(var_datePicker)
        var_datePicker.snp.makeConstraints { make in
            make.width.height.equalTo(256.auto())
            make.center.equalToSuperview()
        }
    }
    func ht_showAnimated() {
        let var_backgroundView = UIView(frame: UIScreen.main.bounds)
        var_backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.4)
        
        let var_maskBtn = UIButton()
        var_maskBtn.backgroundColor = UIColor.clear
        var_maskBtn.addTarget(self, action: #selector(ht_dismissAnimated), for: .touchUpInside)
        var_backgroundView.addSubview(var_maskBtn)
        var_maskBtn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        var_backgroundView.addSubview(self)
        self.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(34)
            make.right.equalToSuperview().offset(-34)
            make.height.equalTo(352.auto())
            make.top.equalToSuperview().offset(HTClassTool.STATIC_navigationBarTotalHeight+10)
            make.centerX.equalToSuperview()
        }
        
        self.alpha = 0.0;
        self.transform = CGAffineTransformMakeScale(0.1, 0.1);
        
        if let var_mainWindow = HTClassTool.STATIC_keyWindow {
            var_mainWindow.addSubview(var_backgroundView)
            UIView.animate(withDuration: 0.3) {
                self.alpha = 1.0
                self.transform = CGAffineTransformIdentity;
            }
        }
    }
    @objc func ht_dismissAnimated() {
        UIView.animate(withDuration: 0.3, animations: {
            self.alpha = 0.0
            self.transform = CGAffineTransformMakeScale(0.1, 0.1);
        }) { finished in
            self.superview?.removeFromSuperview()
        }
    }
}

extension HTClassYearMonthTool: PGDatePickerDelegate {
    func datePicker(_ datePicker: PGDatePicker!, didSelectDate dateComponents: DateComponents!) {
        if var_isSelect {
            if let year = dateComponents!.year, let month = dateComponents!.month {
                BLOCK_selectDateBLOCK?("\(year).\(month)")
            }
        }
        var_isSelect = true
    }
}
